package cn.com.git.workflow.api;


import cn.com.git.common.base.BaseApi;
import cn.com.git.common.model.CommonStateConstant;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import cn.com.git.workflow.common.WorkFlowResultConstant;
import cn.com.git.workflow.config.security.LoginContext;
import cn.com.git.workflow.service.WorkFlowService;
import cn.com.git.workflow.service.dto.*;
import cn.com.git.workflow.service.query.WorkFlowOperateListQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 工作流程
 * @author zhangzhenfu
 * @Description
 * @date 2020/2/22 15:32
 */
@Api(value = "工作流程", description = "工作流程")
@RestController
@RequestMapping("/workflow")
public class WorkflowApi extends BaseApi {

    @Autowired
    private LoginContext loginContext;
    @Autowired
    private WorkFlowService workFlowService;


    @ApiOperation(value = "发起流程", notes = "发起流程", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/start", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<WorkFlowOperateResultDTO> start(@Valid @RequestBody WorkFlowStartDTO form) {
        ResultMsg<WorkFlowOperateResultDTO> resultMsg ;
        try {
            resultMsg = workFlowService.startWorkFlow(form);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(WorkFlowResultConstant.FAILED,e.toString());
            e.printStackTrace();
            logger.error("发起流程失败：{}",e.toString());
        }
        return resultMsg ;
    }

    @ApiOperation(value = "当前节点信息", notes = "当前节点信息", httpMethod = "POST", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/currentNode", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "流程id", required = true, example = "1", paramType = "form", dataType = "int")
    })
    public ResultMsg<WorkFlowCurrentDTO> currentNode(@NotNull(message = "id不能为空") Integer id) {
        ResultMsg<WorkFlowCurrentDTO> resultMsg ;
        try {
            resultMsg = workFlowService.currentWorkFlowNode(id);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(WorkFlowResultConstant.FAILED,e.toString());
            e.printStackTrace();
            logger.error("流程节点查询失败：{}",e.toString());
        }
        return resultMsg ;
    }

    @ApiOperation(value = "流程节点扭转", notes = "流程节点扭转", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/nextNode", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<WorkFlowOperateResultDTO> nextNode(@Valid @RequestBody WorkFlowNextDTO form) {
        ResultMsg<WorkFlowOperateResultDTO> resultMsg ;
        try {
            resultMsg = workFlowService.nextWorkFlow(form);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(WorkFlowResultConstant.FAILED,e.toString());
            e.printStackTrace();
            logger.error("流程节点扭转失败：{}",e.toString());
        }
        return resultMsg ;
    }

    @ApiOperation(value = "流程待办列表查询", notes = "列表查询", httpMethod = "POST",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/operate/list",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operatorId", value = "操作人id", required = true, example = "1", paramType = "form", dataType = "int")
    })
    public ResultPage<WorkFlowOperateListDTO> workFlowOperateList(@NotNull(message = "操作人id不能为空") Integer operatorId) {
        ResultPage<WorkFlowOperateListDTO> result ;
        WorkFlowOperateListQuery query = new WorkFlowOperateListQuery();
        try {
            query.setOperatorId(operatorId);
            query.setState(CommonStateConstant.TRUE.getValue());
            result = workFlowService.listWorkFlowOperate(query);
        }catch (Throwable e){
            result = new ResultPage<>(WorkFlowResultConstant.FAILED);
            e.printStackTrace();
            logger.error("列表查询失败：{}",e.getMessage());
        }
        return result ;
    }

    @ApiOperation(value = "流程操作历史列表查询", notes = "列表查询", httpMethod = "POST",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/operate/history/list",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flowId", value = "流程id", required = true, example = "1", paramType = "form", dataType = "int")
    })
    public ResultPage<WorkFlowOperateListDTO> workFlowOperateHistoryList(@NotNull(message = "流程id不能为空") Integer flowId) {
        ResultPage<WorkFlowOperateListDTO> result ;
        WorkFlowOperateListQuery query = new WorkFlowOperateListQuery();
        try {
            query.setWorkFlowId(flowId);
            query.setState(CommonStateConstant.FALSE.getValue());
            result = workFlowService.listWorkFlowOperate(query);
        }catch (Throwable e){
            result = new ResultPage<>(WorkFlowResultConstant.FAILED);
            e.printStackTrace();
            logger.error("列表查询失败：{}",e.getMessage());
        }
        return result ;
    }

}
